package com.lintf.distributed.transaction.order.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lintf.distributed.transaction.order.entity.Message;
import com.lintf.distributed.transaction.order.mapper.MessageMapper;
import com.lintf.distributed.transaction.order.service.MessageService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author lin tingfeng
 * @ system Mac
 * @ data 2025/6/12 17:35
 * @ package_name com.lintf.distributed.transaction.order.service
 * @ project_name distributed-transaction-demo
 * @ desc
 */
@Service
public class MessageServiceImpl extends ServiceImpl<MessageMapper, Message> implements MessageService {
    @Override
    public List<Message> findMessagesByStatus(Integer status) {
        LambdaQueryWrapper<Message> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Message::getStatus, status);
        return this.list(queryWrapper);
    }

    @Override
    public List<Message> findMessageByTimeout(Integer status, Long timeout) {
        LambdaQueryWrapper<Message> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Message::getStatus, status)
                .lt(Message::getCreatedAt, System.currentTimeMillis() - timeout);
        return this.list(queryWrapper);
    }

    @Override
    public int incrementRetryCount(Message message) {
        message.setRetryCount(message.getRetryCount() + 1);
        return this.baseMapper.updateById(message);
    }
}
